using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Threading;

namespace GoogleNet
{
    /// <summary>
    /// Describes rotation of a 3D model's coordinate system to position the object in Google Earth. 
    /// </summary>
    public class KmlOrientation : IKmlElement
    {
        private string tagName = "Orientation";

        #region Heading
        private double heading = -1;

        /// <summary>
        /// Rotation about the z axis. A value of 0 (the default) equals North. 
        /// A positive rotation is clockwise around the z axis and specified in degrees from 0 to 360.
        /// <remarks>
        /// Compass direction, in degrees. Default=0 (North). 
        /// Values range from 0 to 360 degrees
        /// </remarks>
        /// </summary>
        /// <value>The heading.</value>
        public double Heading
        {
            get { return heading; }
            set
            {
                if (value < 0 || value > 360)
                    throw new InvalidCoordinateValueException("Heading must be between 0 and 360 degrees.");
                else
                    heading = value;
            }
        }
        #endregion

        #region Tilt
        private double tilt;

        /// <summary>
        /// Rotation about the x axis. Default equals 0. 
        /// A positive rotation is clockwise around the x axis and 
        /// specified in degrees from 0 to 360.
        /// </summary>
        /// <value>The tilt.</value>
        public double Tilt
        {
            get { return tilt; }
            set
            {
                if (value < 0 || value > 360)
                    throw new InvalidCoordinateValueException("Tilt must be between 0 and 360 degrees.");
                else
                    tilt = value;
            }
        } 
        #endregion

        #region Roll
        private double roll;

        /// <summary>
        /// Rotation about the y axis. Default equals 0. 
        /// A positive rotation is clockwise around the y axis and specified in degrees from 0 to 360.
        /// </summary>
        /// <value>The roll.</value>
        public double Roll
        {
            get { return roll; }
            set
            {
                if (value < 0 || value > 360)
                    throw new InvalidCoordinateValueException("Roll must be between 0 and 360 degrees.");
                else
                    roll = value;
            }
        } 
        #endregion


        #region IKmlElement Members

        /// <summary>
        /// Name of the KML tag for this specific element.
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        public string TagName
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        #region IKmlGenerator Members

        /// <summary>
        /// Generates the KML for this element including its child elements.
        /// </summary>
        /// <param name="writer">The XML writer that will be used to generate the KML.</param>
        public void GenerateKml(System.Xml.XmlTextWriter writer)
        {
            writer.WriteStartElement(tagName);

            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
            writer.WriteElementString("heading", heading.ToString(Thread.CurrentThread.CurrentCulture.NumberFormat));
            writer.WriteElementString("tilt", tilt.ToString(Thread.CurrentThread.CurrentCulture.NumberFormat));
            writer.WriteElementString("roll", roll.ToString(Thread.CurrentThread.CurrentCulture.NumberFormat));
            Thread.CurrentThread.CurrentCulture = currentCulture;

            writer.WriteEndElement();
        }

        #endregion
    }
}
